import navToNext from '../../../wxjs/navToNext.js'
const app = getApp();


var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  msg: "请稍等...",
};

var currentPage = {
  data,
  form: {},//详情请求参数
  onLoad,
  getDetail,//获取团课详情
  openLocation,
  navToShare,
  previewImage,
  onShareAppMessage: function () {
    const that = this;
    const uuid = that.data.uuid;
    return {
      title: '',
      path: app.xhx_route.leagueList + "?uuid=" + uuid
    }
  }
};

var page = Object.assign(navToNext, currentPage);

Page(page);

function onLoad(options) {
  const that = this;
  const uuid = options && options.uuid || '';

  that.setData({
    uuid
  })
  that.form.uuid = uuid;
  that.getDetail();
}

function getDetail() {
  const that = this;

  app.middleware.requestAjax({
    serAddr: app.serAddrs.getLectureDetail,
    data: that.form,
    cancelToast: true,
    success: function (res) {
      let temp = res.data;
      temp.msg = '';
      temp.label = res.data.label && res.data.label.split(",") || [];
      that.setData(temp)
    },
    fail: function (res) {
      that.setData({
        msg: res && res.msg || "请求失败"
      })
    }
  })
}

function openLocation(event) {
  const that = this;
  const latitude = Number(that.data.latitude);
  const longitude = Number(that.data.longitude);
  app.middleware.openLocation({
    latitude,
    longitude,
  });
}

function navToShare(event){
  const that = this;
  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  that.navToNext(event);
}

function previewImage(event){
  const that = this;
  const index = event.currentTarget.dataset.index;
  let goodsImages = that.data.goodsImages || '[]';
  
  goodsImages = JSON.parse(goodsImages);

  for(let i=0;i<goodsImages.length;i++){
    goodsImages[i] = goodsImages[i].file;
  }

  wx.previewImage({
    current:goodsImages[index],
    urls: goodsImages,
  })

}